package com.itic.appbase.framework.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

public class AutoParam {

    public static Object auto(Class<?> clazz, int length) {
        if (null != clazz && String.class.equals(clazz)) { // String
            return RandomStringUtils.randomAlphanumeric(length);
        }
        return null;
    }

    public static Object auto(Class<?> clazz, Object start, Object end,
            String style) {
        if (null != clazz && null != start && null != end && null != style) {
            DecimalFormat df = new DecimalFormat();
            if (Double.class.equals(clazz)) { // double
                Double d = RandomUtils.nextDouble((Double) start, (Double) end);
                df.applyPattern(style);
                return df.format(d);
            }
            if (Float.class.equals(clazz)) { // float
                Float f = RandomUtils.nextFloat((Float) start, (Float) end);
                df.applyPattern(style);
                return df.format(f);
            }
            if (Long.class.equals(clazz)) { // long
                Long l = RandomUtils.nextLong((Long) start, (Long) end);
                df.applyPattern(style);
                return df.format(l);
            }
            if (Date.class.equals(clazz)) { // date
                return randomDate((String) start, (String) end, style);
            }
        }
        return null;
    }
    
    public static Object auto
    (Class<?> clazz, Object start, Object end) {
        if (null != clazz && null != start && null != end){
            if (Integer.class.equals(clazz)){ // int
                return RandomUtils.nextInt((Integer)start, (Integer)end);
            }
        }
        return null;
    }
    
    public static Object auto(Class<?> clazz) {
        if (null != clazz && boolean.class.equals(clazz)) {
            return RandomUtils.nextInt(1, 3) % 2 == 0;
        }
        return null;
    }

    private static Date randomDate(String beginDate, String endDate,
            String style) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(style);
            Date start = format.parse(beginDate);// 开始日期
            Date end = format.parse(endDate);// 结束日期
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());

            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin, long end) {
        long rtnn = begin + (long) (Math.random() * (end - begin));
        if (rtnn == begin || rtnn == end) {
            return random(begin, end);
        }
        return rtnn;
    }
    
    public static Object auto(Object[] params) {
       return null != params ? params[RandomUtils.nextInt(0, params.length)] : null;
    }

}
